package com.naiterui.ehp.bs.patient.controller.inner;

import com.naiterui.ehp.bp.bo.cms.FollowUpRecordFormBO;
import com.naiterui.ehp.bp.bo.emr.QaPageBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.patient.service.IApFollowUpRecordService;
import com.naiterui.ehp.bs.patient.service.IApQaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 随访管理通知
 *
 * @author yangfuya
 * @since 2022/1/12
 */
@Slf4j
@Api(tags = {SwaggerTag.PATIENT + SwaggerTag.INNER_API + SwaggerTag.FOLLOW_UP})
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("ap/inner/followup")
public class ApInnerFollowUpController {
    private final IApFollowUpRecordService followUpRecordService;
    private final IApQaService apQaService;

    /**
     * 随访计划到制定日期的早上10点发送订阅消息
     *
     * @throws BusinessException
     */
    @PostMapping("/start")
    public void followUpCallback() throws BusinessException {
        log.info("[定时] 随访计划到制定日期的早上10点发送订阅消息");
        this.followUpRecordService.followUpStarted();
    }

    /**
     * 随访计划结束
     *
     * @throws BusinessException
     */
    @PostMapping("/finish")
    public void followUpFinishCallback() throws BusinessException {
        log.info("[定时] 随访计划结束 ");
        this.followUpRecordService.followUpFinish();
    }

    /**
     * 查询问卷统计列表
     *
     * @return
     */
    @PostMapping("qa")
    @ApiOperation(value = "查询问卷统计列表")
    public ResponseVO<QaPageBO> qaRecord(@RequestBody List<FollowUpRecordFormBO> records) throws BusinessException {
        QaPageBO qaRecordBO = apQaService.qaRecord(records);
        return PublicService.returnResponseVO(qaRecordBO);
    }

    /**
     * 问卷导出API
     *
     * @param records
     * @return
     * @throws BusinessException
     */
    @PostMapping("qa/export")
    @ApiOperation(value = "问卷导出API")
    public ResponseVO<byte[]> qaRecordExport(@RequestBody List<FollowUpRecordFormBO> records) throws BusinessException {
        return PublicService.returnResponseVO(apQaService.qaRecordExport(records));
    }
}
